# scan_question.py
# usage: 扫描问题清单，看能够提炼多少共性问题
# python ./src/tools/scan_question.py --src ./data/test.jsonl


from typing import List, Dict
import os, sys
import json
import argparse

# 假设 project_root 已设置
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, project_root)

from query.query_table import query_table
from tools.clean_question import clean_question


def main():
    parser = argparse.ArgumentParser(description="扫描问题文件，调用 query_table，收集未命中问题并清洗。")
    parser.add_argument('--src', required=True, help='输入的问题文件路径（每行为一个JSON对象）')

    args = parser.parse_args()

    new_questions: List[str] = []                                # 存储原始问题（可选）
    new_clean_to_info: Dict[str, dict] = {}                      # cleaned_question -> 原始信息（id, question, level）

    old_questions_path = "./data/questions_old.jsonl"

    try:
        with open(args.src, 'r', encoding='utf-8') as f:
            line_num = 0
            for line in f:
                line_num += 1
                line = line.strip()
                if not line:
                    continue
                try:
                    data = json.loads(line)
                    question_id = data.get("id")
                    question = data.get("question", "").strip()
                    level = data.get("level")

                    if not question:
                        print(f"警告: 第 {line_num} 行 id={question_id} 的 question 为空，跳过。")
                        continue

                    # 调用 query_table 函数
                    result = query_table(question)

                    if result is None:
                        new_questions.append(question)

                        # 清洗问题
                        cleaned = clean_question(question)
                        if not cleaned:
                            continue

                        # 如果这个 cleaned question 是第一次出现，记录其原始信息
                        if cleaned not in new_clean_to_info:
                            new_clean_to_info[cleaned] = {
                                "id": question_id,
                                "question": question,
                                "level": level
                            }
                    else:
                        # result is not None：表示命中，写入 old 文件
                        record_to_write = {
                            "id": question_id,
                            "question": question,
                            "level": level,
                            "result": result  # 可选：将 result 也保存
                        }
                        with open(old_questions_path, 'a', encoding='utf-8') as old_file:
                            old_file.write(json.dumps(record_to_write, ensure_ascii=False) + '\n')

                except json.JSONDecodeError as e:
                    print(f"第 {line_num} 行 JSON 解析错误: {e}")
                except Exception as e:
                    print(f"处理第 {line_num} 行时发生异常: {e}")

    except FileNotFoundError:
        print(f"错误: 文件 '{args.src}' 未找到。")
        sys.exit(1)
    except Exception as e:
        print(f"读取文件时发生错误: {e}")
        sys.exit(1)

    # 输出统计和明细
    print("\n" + "="*60)
    print("结果汇总")
    print("="*60)

    print(f"\n【new_questions】累计笔数: {len(new_questions)}")
    print("【new_questions】明细:")
    for idx, q in enumerate(new_questions, 1):
        print(f"  {idx}. {q}")

    print(f"\n【new_clean_questions】去重后笔数: {len(new_clean_to_info)}")
    print("【new_clean_questions】明细:")
    for idx, cleaned_q in enumerate(sorted(new_clean_to_info.keys()), 1):
        info = new_clean_to_info[cleaned_q]
        example_str = f"id={info['id']}; question={info['question']}; level={info['level']}"
        print(f"  {idx}. {cleaned_q}（例如：{example_str}）")

    print(f"\n命中问题已保存至: {old_questions_path}")


if __name__ == "__main__" :
    main()
